package com.taxi.driver.module.order.cancel;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.qianxx.utils.TypeUtils;
import com.qianxx.view.HeadView;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.BizStatus;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.module.order.cancel.dagger.DaggerOrderCancelComponent;
import com.taxi.driver.module.order.cancel.dagger.OrderCancelModule;
import com.taxi.driver.module.vo.CancelDesVO;
import com.taxi.driver.module.vo.CancelReasonVO;
import com.taxi.driver.socket.SocketEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 功能描述：
 */
public class OrderCancelActivity extends BaseActivity implements OrderCancelContract.View {

    public static void actionStart(Context context, String orderUuid) {
        Intent intent = new Intent(context, OrderCancelActivity.class);
        intent.putExtra(IConstants.ORDER_UUID, orderUuid);
        context.startActivity(intent);
    }

    @Inject
    OrderCancelPresenter mPresenter;
    OrderCancelAdapter mAdapter;

    @BindView(R.id.head_view)
    HeadView mHeadView;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    ViewHolder mHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_cancel);
        ButterKnife.bind(this);
        DaggerOrderCancelComponent.builder().appComponent(getAppComponent())
                .orderCancelModule(new OrderCancelModule(this)).build().inject(this);
        mPresenter.setOrderUuid(getIntent().getStringExtra(IConstants.ORDER_UUID));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        View topView = LayoutInflater.from(this).inflate(R.layout.layout_tv_view, mRecyclerView, false);
        mHolder = new ViewHolder(topView);
        mHolder.mTvTag.setText(R.string.order_cancel_reason);
        View bottomView = LayoutInflater.from(this).inflate(R.layout.layout_btn_view, mRecyclerView, false);
        bottomView.findViewById(R.id.tv_submit).setOnClickListener(v -> {
            mPresenter.submitCancel(mAdapter.getContent()); //取消订单
        });
        mAdapter = new OrderCancelAdapter(this, R.layout.item_order_cancel);
        mAdapter.addHeaderView(topView);
        mAdapter.addFooterView(bottomView);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnClickListener(R.id.layout, (position, view, item) -> {
            mAdapter.select(position); //刷新显示
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.unsubscribe();
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void showCancelMsg(List<CancelReasonVO> vos) {
        mAdapter.setAll(vos); //刷新显示
    }

    @Override
    public void cancelSuccess(String orderUuid) {
        toast("订单已取消");
        // 营运状态（空驶）
        EventBus.getDefault().post(new SocketEvent(SocketEvent.BIZ_STATUS, BizStatus.EMPTY_DRIVING));
        // 关闭界面
        finish();
    }

    @Override
    public void showCancelDes(CancelDesVO vo) {
        if (TextUtils.isEmpty(vo.details)) {
            gone(mHolder.mTvTitle, mHolder.mTvDetail);
        } else {
            visible(mHolder.mTvTitle, mHolder.mTvDetail);
            mHolder.mTvTitle.setText(TypeUtils.getValue(vo.description));
            mHolder.mTvDetail.setText(TypeUtils.getValue(vo.details));
        }
    }

    static class ViewHolder {
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_detail)
        TextView mTvDetail;
        @BindView(R.id.tv_tag)
        TextView mTvTag;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
